// pages/ordersubmit/ordersubmit.js
import {getStorage} from '../../utils/storage'
import {reqDefaultAddress, reqPickupList} from '../../api/ordersubmit'
import { reqAddress } from '../../api/address'
import { reqOrderAddressEdit } from '../../api/orderdetail'


Page({

  /**
   * 页面的初始数据
   */
  data: {
    ordersubmitgoods:[],
    totalprice:0,
    address:{},
    addressshow:false,
    pickupshow:false,
    addressList:[],
    pickupList:[],
    pickup:{},
    active:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const ordersubmitgoods = getStorage('ordersubmitgoods')

    let totalprice = 0
    for (let i = 0; i < ordersubmitgoods.length;i++){
      totalprice += ordersubmitgoods[i].priceTotal
    }

    totalprice*=100

    this.setData({
      ordersubmitgoods:ordersubmitgoods,
      totalprice:totalprice
    })
  },

  async onShow(){
    const {data} = await reqDefaultAddress()
    console.log(data)

    this.setData({
      address:data
    })

    this.getAddressList()
    this.getPickupList()
  },

  back(){
    wx.navigateBack()
  },

  onopenaddress(){
    this.setData({
      addressshow:true
    })
  },

  oncloseaddress(){
    this.setData({
      addressshow:false
    })
  },

  async getAddressList(){
    const {data} =await reqAddress()
    this.setData({
      addressList:data
    })
  },

  async changeaddress(e){
    const id = e.currentTarget.dataset.id

    const active = this.data.active

    if (active == 0){
      const list = this.data.addressList
    for (let i = 0;i < list.length;i++){
      if (list[i].addressId == id){
        this.setData({
          address:list[i]
        })
        break
      }
    }
      const param = {
        orderId:this.data.ordersubmitgoods[0].orderId,
        addressId:this.data.address.addressId
      }
      await reqOrderAddressEdit(param)

      this.oncloseaddress()
    }else{

      const list = this.data.pickupList
    for (let i = 0;i < list.length;i++){
      if (list[i].addressId == id){
        this.setData({
          pickup:list[i]
        })
        break
      }
    }
      const param = {
        orderId:this.data.ordersubmitgoods[0].orderId,
        addressId:this.data.pickup.addressId
      }
      await reqOrderAddressEdit(param)

      this.onclosepickup()
    }


  },

  async getPickupList(){
    const {data} = await reqPickupList()
    this.setData({
      pickupList:data,
      pickup:data[0]
      //TODO 默认团购地址需结合团购业务考虑
    })
  },

  onopenpickup(){
    this.setData({
      pickupshow:true
    })
  },

  onclosepickup(){
    this.setData({
      pickupshow:false
    })
  },

  // 当用户更改导航栏时 更改订单的地址
  async onchangeactive(e){
    const active = e.detail.name
    this.setData({
      active:active
    })

    if (active == 0){
      const param = {
        orderId:this.data.ordersubmitgoods[0].orderId,
        addressId:this.data.address.addressId
      }
      await reqOrderAddressEdit(param)
    }else{
      const param = {
        orderId:this.data.ordersubmitgoods[0].orderId,
        addressId:this.data.pickup.addressId
      }
      await reqOrderAddressEdit(param)
    }
  }

})